package com.andaily.web.controller.mobile.account;

import com.andaily.domain.dto.account.AccountSearchDTO;
import com.andaily.domain.dto.account.AccountSearchResultDTO;
import com.andaily.domain.dto.account.AccountTypeDTO;
import com.andaily.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author Shengzhao Li
 */
@Controller
@RequestMapping("/account/search")
public class AccountSearchController {

    private AccountService accountService;

    @RequestMapping("init")
    public String setForm(AccountSearchDTO accountSearchDTO, ModelMap model) throws Exception {
        model.addAttribute("accountSearch", accountSearchDTO);
        List<AccountTypeDTO> accountTypes = accountService.loadAvailableAccountTypes();
        model.addAttribute("accountTypes", accountTypes);
        return "account/account_search";
    }

    @RequestMapping("submit")
    public String submit(@ModelAttribute("accountSearch") AccountSearchDTO accountSearchDTO,
                         ModelMap model) throws Exception {

        AccountSearchResultDTO accountSearchResult = accountService.searchAccount(accountSearchDTO);
        model.addAttribute("accountSearchResult", accountSearchResult);
        return "account/account_search_result";
    }

    @RequestMapping("more")
    public String more(AccountSearchDTO accountSearchDTO, ModelMap model) throws Exception {
        AccountSearchResultDTO accountSearchResult = accountService.searchAccount(accountSearchDTO);
        model.addAttribute("accountSearchResult", accountSearchResult);
        return "account/account_search_result_more";
    }

    @Autowired
    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }
}
